package com.rtx.wms.customer.k3.base.service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.rtx.edi.model.field.po.PlFieldMapping;
import com.rtx.edi.platform.field.service.PlFieldMappingService;
import com.rtx.edi.platform.synctask.service.GatWayService;
import com.rtx.wms.customer.k3.api.K3Api;

/**
 * 基础数据同步
 * @author A
 *
 */
public abstract class AbsBasicSchedulerAPI {
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Resource(name = "ApiGatWayService")
	GatWayService gatWayService;
	
	@Autowired
	protected K3Api k3Api;
	
	@Autowired
	PlFieldMappingService plFieldMappingService;

	public void schedulerBasic() {
		try {
			LambdaQueryWrapper<PlFieldMapping> qw = Wrappers.lambdaQuery();
			qw.eq(PlFieldMapping::getCode, getConfigCode());
			List<PlFieldMapping> list = plFieldMappingService.queryByCondition("ris_edi", qw);
			if(list==null || list.size()<1) {
				return;
			}
			PlFieldMapping config = list.get(0);
			int maxsize= Integer.parseInt(Optional.ofNullable(config.getUdf1()).orElse("60000") );
			int rowsize = Integer.parseInt(Optional.ofNullable(config.getUdf2()).orElse("200") );
			int rowcount = maxsize%rowsize>0?maxsize/rowsize+1:maxsize/rowsize;
			
			int startRow = 0;
			for(int i = 0;i<rowcount;i++) {
				String responseStr = queryBasicData(startRow,rowsize,maxsize);
				List<String> requestList = JSON.parseArray(responseStr, String.class);
				
				callWmsAPI(requestList);
				
				startRow += rowsize;
			}
			
		} catch (Exception e) {
			log.error("",e);
		}
	};
	
	
	
	protected void callWmsAPI(List<String> requestList) {
		if(requestList == null || requestList.size() < 1) {
			return;
		}
		for(String request:requestList) {
			gatWayService.handel(getWmsAPIMappingCode(), request);
		}
	};
	
	protected String queryBasicData(int startRow,int topRowCount,int maxcount) throws Exception {
		String uid = UUID.randomUUID().toString();
		K3CloudApi api = k3Api.getK3API();
		
		Map<String,Object> request = new JSONObject();
		request.put("FormId", getK3FormId());
		request.put("FieldKeys", getK3Fields());
		request.put("FilterString", "");
		request.put("OrderString", "");
		request.put("TopRowCount", maxcount);
		request.put("StartRow", startRow);
		request.put("Limit", topRowCount);
		request.put("SubSystemId", "");
		
		String requestStr = JSON.toJSONString(request);
		
		log.info("uid-{}-请求K3-{}-主数据请求-{}",uid,getK3FormId(),requestStr);
		String result = api.billQuery(requestStr);
//		log.info("uid-{}-请求K3-{}-主数据响应-{}",uid,getK3FormId(),result);
		return result;
	}
	
	/**
	 * 获取配置pl_Field_Mapping拉取数据配置项
	 * @return
	 */
	protected abstract String getConfigCode();
	
	/**
	 * 获取配置K3基础数据必传参数FormId
	 * @return
	 */
	protected abstract String getK3FormId();
	
	/**
	 * 获取配置K3基础数据必传参数返回字段列表
	 * @return
	 */
	protected abstract String getK3Fields();
	
	/**
	 * 获取edi转发对接场景编码
	 * @return
	 */
	protected abstract String getWmsAPIMappingCode();

}
